package com.it.smartoldb.controller;

import com.it.smartoldb.common.Result;
import com.it.smartoldb.dto.LoginRequest;
import com.it.smartoldb.dto.LoginResponse;
import com.it.smartoldb.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 */
@Tag(name = "认证管理", description = "登录、注册、退出等认证相关接口")
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
    
    private final AuthService authService;
    
    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "根据用户名密码和角色进行登录认证")
    @PostMapping("/login")
    public Result<LoginResponse> login(@Validated @RequestBody LoginRequest request) {
        LoginResponse response = authService.login(request);
        return Result.success("登录成功", response);
    }
    
    /**
     * 用户退出
     */
    @Operation(summary = "用户退出", description = "退出登录，清除token")
    @PostMapping("/logout")
    public Result<Void> logout() {
        authService.logout();
        return Result.success();
    }
    
    /**
     * 获取当前用户信息
     */
    @Operation(summary = "获取当前用户信息", description = "根据token获取当前登录用户信息")
    @GetMapping("/me")
    public Result<LoginResponse> getCurrentUser() {
        LoginResponse response = authService.getCurrentUser();
        return Result.success(response);
    }
} 